/*
 * @Author: wwssaabb
 * @Date: 2021-07-15 09:32:51
 * @LastEditTime: 2021-07-15 09:33:13
 * @FilePath: \demo\crawler_demo\电影天堂_demo\history.js
 */


async function getHTML(http) {
  let {data}=await axios(http,{responseType:'arraybuffer'})
  //let res=await axios(http,{responseType:'arraybuffer'})
  let html=iconv.decode(data,'gb2312')
  fs.writeFileSync('./html/index.html',html)
}
function replaceText(path,str,text){
  let context=fs.readFileSync(path,'utf8')
  if(Array.isArray(str)){
    str.forEach(i=>{
      context=context.replace(new RegExp(i,'g'),text)
    })
  }else{
    context=context.replace(new RegExp(str,'g'),text)
  }
  //fs.writeFileSync('./html/index2.html',context)
  return context
}

//获取首页列表数据
//getHTML(httpUrl)
/* let html=replaceText('./html/index.html','co_content3','co_content8')
const $=cheerio.load(html)
let index_arr=[] 

$('.co_area2').each(function(){
  if($('div',this).last().attr('class')==='co_content8'){
    let more_href=baseurl+$('.title_all a',this).attr('href')
    $('.title_all p em',this).remove()
    let list=[]
    let title=$('.title_all p',this).text()
    $('.co_content8 tr',this).each(function(){
      let tag,name,href
      $('td.inddline',this).each(function(i){
        if(i===1) return 
        $('a',this).each(function(i){
          console.log(i)
          if(i===0){
            tag=$(this).text()
          }else if(i===1){
            name=$(this).text()
            href=baseurl+$(this).attr('href')
          }
        })
        pubdate=$(this).next().text()
        list.push({tag,name,href,pubdate})
      })
      
    })
    index_arr.push({title,list,more_href})
  }else{
    let title=$('.title_all p',this).text()
    let list=[]
    $('ul a',this).each(function(){
      let name=$(this).text()
      let href=baseurl+$(this).attr('href')
      list.push({name,href})
    })
    index_arr.push({title,list})
  }
}) */

//fs.writeFileSync('./json/index.json',JSON.stringify(index_arr))

//获取国内电影、日韩、欧美、精品
chain_films={} 
jk_films={}
ea_films={}
boutique_films={}
/* let _films={
  4:'chain_films',
  5:'jk_films',
  6:'ea_films',
  10:'boutique_films',
} */
let all_films=[{'chain_films':chain_films},{'jk_films':jk_films},{'ea_films':ea_films},{'boutique_films':boutique_films}]
/* for(let i=0;i<=10;i++){
  if(i===4){
    i=10
  }
  axios.get(index_arr[i].more_href,{responseType:'arraybuffer'}).then(({data})=>{
    let html=iconv.decode(data,'gb2312')
    fs.writeFileSync('./html/'+_films[i]+'.html',html)
  })
} */
for(let obj of all_films){
  let name=Object.keys(obj)[0]
  //console.log(name)
  let context=fs.readFileSync('./html/'+name+'.html','utf8')
  obj[name].context=context
}
//console.log(chain_films.context) 

//获取国内电影全部页数地址
function getPage(_this,baseurl){
  const $=cheerio.load(_this.context)
  let page_arr=[]
  $('.x select option').each(function(){
    page_arr.push(baseurl+'/'+$(this).attr('value'))
  })
  _this.page_arr=page_arr
}
getPage(chain_films,'https://www.dytt8.net/html/gndy/china')
console.log(chain_films.page_arr) 

//遍历页数获取全部电影
function getFilms(_this,baseurl='https://www.dytt8.net'){
  let films=[]
  function getPageFilm(http) {
    axios.get(http,{response:'arraybuffer'}).then(({data})=>{
      let context=iconv.decode(data,'gb2312')
      const $=cheerio.load(context)
      $('.co_content8 td').each(function(){
        $('a',this).each(function(i){
          if(i===1){
            console.log($(this).attr('href'))
            films.push(baseurl+$(this).attr('href'))
            if(films.length!==0&&films.length===_this.page_arr.length){
              console.log(films)
              return 
            }
            setTimeout(()=>getPageFilm(_this.page_arr[films.length]),1000)
          }
        })
      })
    })
    
  }
  getPageFilm(_this.page_arr[0])
}

//getFilms(chain_films)
//console.log(index_arr[4].more_href)


//html=fs.readFileSync('./html/test.html','utf8')

//const $=cheerio.load(html)

//console.log(getInfo($('#Zoom span').html()))